// Step 2: Add the English words package and generate a word pairing each
// time the app is hot reloaded.

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/util/client.dart';
import 'package:flutter_app/model/SearchModel.dart';

import 'package:flutter_app/page/product.dart';



class HomeWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _TextFieldState();
  }
}

class _TextFieldState extends State<HomeWidget> {
  bool _loading = false;

  int _page = 0;

  List<SearchModelEntries> _entries = [];

  ScrollController _scrollController = new ScrollController(); //滚动监听

  TextEditingController _userEtController = TextEditingController();

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        _getMoreData();
      }
    });
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  _getMoreData() async {
    if (!_loading) {
      setState(() {
        _loading = true;
      });

      _page++;

      Client.post("http://wxapp.qqvbc.com/crab/v2/search", params: {
        "kw": _userEtController.text,
        "sort": "quan",
        "page": _page.toString(),
        "limit": "5",
      }).then((s) {
        setState(() {
          _loading = false;
          SearchModel _searchModel = SearchModel(s);
          _entries.addAll(_searchModel.data.data);
          print("_searchModel.data.data ");
          print(_searchModel.data.data.length);
          _page = _searchModel.data.page;
        });
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("好礼购"),
      ),
      body: new Container(
        padding: EdgeInsets.all(10),
        child: new Column(
          children: <Widget>[
            new Row(children: <Widget>[
              new Expanded(
                  child: new TextField(
                decoration: InputDecoration(hintText: "请输入关键词"),
                controller: _userEtController,
                textInputAction: TextInputAction.search,
              )),
              new IconButton(
                icon: Icon(Icons.search),
                color: Colors.blueAccent,
                highlightColor: Colors.red,
                onPressed: () {
                  setState(() {
                    _onSearch();
                  });
                },
              ),
            ]),
            new Expanded(
                child: new Container(
              child: _buildList(),
            )),
          ],
        ),
      ),
    );
  }

  Widget _buildList() {
    return new ListView.builder(
        controller: _scrollController,
        itemCount: _entries.length + 1,
        shrinkWrap: true,
        itemBuilder: (BuildContext context, int i) {
          if (i >= _entries.length) {
            return _buildProgressIndicator();
          } else {
            return _buildLine(entry: _entries[i], context: context);
          }
        });
  }

  Widget _buildLine({SearchModelEntries entry, BuildContext context}) {
    return new GestureDetector(
        onTap: () {
          //传参数跳转
          Future future = Navigator.push(context, PageRouteBuilder(pageBuilder:
              (BuildContext context, Animation<double> animation,
              Animation<double> secondaryAnimation) {
            return ProductWidget(id: entry.num_iid);
          }));

          //Navigator.of(context).pushNamed('/product');
        },
        child: new Container(
            padding: const EdgeInsets.all(5.0),
            child: new Row(children: [
              new Expanded(
                flex: 1,
                child: new Image.network(
                  entry.pict_url,
                  height: 100,
                ),
              ),
              new Expanded(
                  flex: 2,
                  child: new Container(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 10, vertical: 0.0),
                      child: new Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          new Container(
                              height: 70,
                              child: new Text(entry.title,
                                  style: new TextStyle(
//                                fontSize: 18.0, //字体大小,
                                      ))),
                          new Stack(
                            children: [
                              new Text("天猫价 ¥" + entry.zk_price.toString(),
                                  style: new TextStyle(
                                      color: const Color(0xaaaaaaaa))),
                              new Align(
                                alignment: FractionalOffset.centerRight,
                                child: new Text(
                                    "已售 " + entry.volume.toString() + "件",
                                    style: new TextStyle(
                                        color: const Color(0xaaaaaaaa))),
                              )
                            ],
                          ),
                          new Stack(
                            children: [
                              new Text("劵后价 ¥" + entry.qh_price.toString(),
                                  style: new TextStyle(
                                      color: const Color(0xffff0000))),
                              new Align(
                                alignment: FractionalOffset.centerRight,
                                child: new Text(
                                    "¥" + entry.coupon_money.toString() + "元劵",
                                    style: new TextStyle(
                                        color: const Color(0xffff0000))),
                              ),
                            ],
                          )
                        ],
                      ))),
            ])));
  }

  Future<Null> _onSearch() async {
    setState(() {
      _entries.clear();
    });
    _getMoreData();
  }

  Widget _buildProgressIndicator() {
    return new Padding(
      padding: const EdgeInsets.all(8.0),
      child: new Center(
        child: new Opacity(
          opacity: _loading ? 1.0 : 0.0,
          child: new CircularProgressIndicator(),
        ),
      ),
    );
  }
}
